package test;

import com.backblaze.erasure.fec.Snmp;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import kcp.*;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 测试大量连接不通讯的例子
 * Created by JinMiao
 * 2019-07-10.
 */
@Slf4j
public class KcpIdleExampleClient implements KcpListener {

    public static void main(String[] args) {

        ChannelConfig channelConfig = new ChannelConfig();
        channelConfig.nodelay(true,40,2,true);
        channelConfig.setSndwnd(1024);
        channelConfig.setRcvwnd(1024);
        channelConfig.setMtu(1400);
        //channelConfig.setFecDataShardCount(10);
        //channelConfig.setFecParityShardCount(3);
        channelConfig.setAckNoDelay(false);
        channelConfig.setCrc32Check(true);
        //channelConfig.setTimeoutMillis(10000);

        KcpClient kcpClient = new KcpClient();
        kcpClient.init(channelConfig);


        /*for (int i = 0; i < 3; i++) {
            if(i%1000==0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            KcpIdleExampleClient kcpIdleExampleClient = new KcpIdleExampleClient();
            kcpClient.connect(new InetSocketAddress("192.168.0.101", 10020), channelConfig, kcpIdleExampleClient);
        }*/

        KcpIdleExampleClient kcpIdleExampleClient = new KcpIdleExampleClient();
        kcpClient.connect(new InetSocketAddress("192.168.0.101", 10020), channelConfig, kcpIdleExampleClient);

    }
    int i =0;

    @Override
    public void onConnected(Ukcp ukcp) {
//        ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(124);
//        byteBuf.writeInt(i++);
//        byte[] bytes = new byte[120];
//        byteBuf.writeBytes(bytes);
//        ukcp.write(byteBuf);
//        byteBuf.release();

        String str = "Hello, World!";
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuf byteBuf = Unpooled.copiedBuffer(str, utf8);
        ukcp.write(byteBuf);
    }
    int j =0;

    @Override
    public void handleReceive(ByteBuf byteBuf, Ukcp ukcp) throws InterruptedException {
        ukcp.write(byteBuf);
        int id = byteBuf.getInt(0);
        //if(j-id%10!=0){
        //    log.info("id"+id +"  j" +j);
        //}
        //随机休眠500以内的毫秒数
        Random random = new Random();
        int i = random.nextInt(50);
        TimeUnit.MILLISECONDS.sleep(i);

        //log.info("client receive:  " + byteBuf.toString(Charset.forName("UTF-8")));

        j++;
        if(j%100000==0){
            log.info(Snmp.snmp.toString());
            log.info("收到了 返回回去"+j);
        }
    }

    @Override
    public void handleException(Throwable ex, Ukcp kcp) {
        ex.printStackTrace();
    }

    @Override
    public void handleClose(Ukcp kcp) {
        log.info("连接断开了");
    }


}